using Senparc.Weixin.Entities;
using Senparc.Weixin.Helpers;
using Senparc.Weixin.HttpUtility;
using System;
using System.IO;
using System.Text;
namespace Senparc.Weixin.MP.CommonAPIs
{
	public static class CommonJsonSend
	{
		public static WxJsonResult Send(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = 10000)
		{
			return CommonJsonSend.Send<WxJsonResult>(accessToken, urlFormat, data, sendType, timeOut, false);
		}
		public static T Send<T>(string accessToken, string urlFormat, object data, CommonJsonSendType sendType = CommonJsonSendType.POST, int timeOut = 10000, bool checkValidationResult = false)
		{
			string url = string.IsNullOrEmpty(accessToken) ? urlFormat : string.Format(urlFormat, accessToken);
			switch (sendType)
			{
			case CommonJsonSendType.GET:
			{
				T result = Get.GetJson<T>(url, null);
				return result;
			}
			case CommonJsonSendType.POST:
			{
				SerializerHelper serializerHelper = new SerializerHelper();
				string jsonString = serializerHelper.GetJsonString(data);
				using (MemoryStream memoryStream = new MemoryStream())
				{
					byte[] bytes = Encoding.UTF8.GetBytes(jsonString);
					memoryStream.Write(bytes, 0, bytes.Length);
					memoryStream.Seek(0L, SeekOrigin.Begin);
					T result = Post.PostGetJson<T>(url, null, memoryStream, null, timeOut, checkValidationResult);
					return result;
				}
				break;
			}
			}
			throw new ArgumentOutOfRangeException("sendType");
		}
	}
}
